package it.polimi.serviceTech.email;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.github.sendgrid.SendGrid;

public class NotificationEmail extends HttpServlet{
	private static final long serialVersionUID = 1L;

	// Constants
	private static String from = "admin@tivendoio.it";
	private static String subjectSeller = "Item Sold!";
	private static String subjectBuyer = "Item Bought!";
	private static String bodySeller = "<h1>Hi, you sold an item!</h1></br>";
	private static String bodyBuyer = "<h1>Hi, you bought an item!</h1></br>";
	private static String footer = "<h1>Thank you for using TiVendoIo, the staff.</h1>";
	private static String username = "dylaniato";
	private static String password = "deni1990";

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		// Get parameters
		String sellerEmail = request.getParameter("seller");
		String buyerEmail = request.getParameter("buyer");
		String itemId = request.getParameter("id");
		
		// Setting email parameters
		SendGrid sendgrid = new SendGrid(username, password);
		sendgrid.addTo(sellerEmail);
		sendgrid.setFrom(from);
		sendgrid.setSubject(subjectSeller);
		sendgrid.setHtml(bodySeller + itemId + footer);
			
		// Sending email
		sendgrid.send();
		
		// Setting email parameters
		sendgrid.addTo(buyerEmail);
		sendgrid.setFrom(from);
		sendgrid.setSubject(subjectBuyer);
		sendgrid.setHtml(bodyBuyer + itemId + footer);
					
		// Sending email
		sendgrid.send();
		
		System.out.println("Mails sent!");
		}
	
}
